/**
 * <pre>
 * 메인 스크립트
 * </pre>
 * 
 * @namespace
 * @author 김왕기
 * @version 1.0(2013. 03. 07) 최초 생성
 */
var Main = {
    /**
     * GNB 시작 위치
     * 
     * @property
     * @type Array
     */
    gnbTop: [ 800, 2800, 5500, 7500 ],
    
    /**
     * GNB 컨텐츠 위치
     * 
     * @property
     * @type Array
     */
    gnbCon: [ 1300, 3100, 5600, 9400 ],
    
    /**
     * GNB 이동
     * 
     * @function
     * @param {String} hash 해시 값
     */
    moveGnb: function( hash ) {
        var gnbCon = Main.gnbCon;
        
        switch ( hash ) {
        case '#info': window.scrollTo( 0, gnbCon[ 0 ] ); break;
        case '#down': window.scrollTo( 0, gnbCon[ 1 ] ); break;
        case '#part': window.scrollTo( 0, gnbCon[ 2 ] ); break;
        case '#used': window.scrollTo( 0, gnbCon[ 3 ] ); break;
        }
        
        Common.animateLogo( hash === '#used' ? 'b' : 'a' );
    },
    
    /**
     * GNB UI 업데이트
     * 
     * @function
     */
    updateGnbUI: function() {
        Main.updateGnbUI = (function() {
            var $window = $( window ),
                $gnbwrap = $( '#header .gnb_wrap' ),
                $gnbmenu = $gnbwrap.find( '.gnb li' );
            
            return function() {
                var scrollTop = $window.scrollTop(),
                    gnbTop = Main.gnbTop;
                
                if ( scrollTop >= gnbTop[ 3 ] ) {
                    $gnbwrap.removeClass( 'type_a type_b' ).addClass( 'type_b' );
                    $( '#d_logo_a' ).hide();
                    $( '#d_logo_b' ).show();
                } else {
                    $gnbwrap.removeClass( 'type_a type_b' ).addClass( 'type_a' );
                    $( '#d_logo_a' ).show();
                    $( '#d_logo_b' ).hide();
                }
                
                if ( false ) {
                } else if ( scrollTop >= gnbTop[ 3 ]  ) {
                    $gnbmenu.eq( 3 ).addClass( 'on' ).siblings().removeClass( 'on' );
                } else if ( scrollTop >= gnbTop[ 2 ] ) {
                    $gnbmenu.eq( 2 ).addClass( 'on' ).siblings().removeClass( 'on' );
                } else if ( scrollTop >= gnbTop[ 1 ] ) {
                    $gnbmenu.eq( 1 ).addClass( 'on' ).siblings().removeClass( 'on' );
                } else if ( scrollTop >= gnbTop[ 0 ] ) {
                    $gnbmenu.eq( 0 ).addClass( 'on' ).siblings().removeClass( 'on' );
                } else {
                    $gnbmenu.removeClass( 'on' );
                }
            };
        })();
    },
    
    /**
     * 앱 탭 변경
     * 
     * @function
     * @param {String} index 순서
     */
    changeAppTab: function( index ) {
        if ( ! Main._$appMenu ) {
            Main._$appMenu = $( '#d_app_tab li' );
            Main._$appVisu = $( '.section3 .visual' );
            Main._$appCont = $( '.section3 .lte_info' );
            Main._$appPrev = $( '#d_app_prev' );
            Main._$appNext = $( '#d_app_next' );
            Main._appIndex = 0;
        }
        
        index = index % Main._$appMenu.length;
        
        if ( Main._appIndex !== index && ! Main._$appVisu.is( ':animated' ) ) {
            var $ov = Main._$appVisu.eq( Main._appIndex ),
                $nv = Main._$appVisu.eq( index ),
                $oc = Main._$appCont.eq( Main._appIndex ),
                $nc = Main._$appCont.eq( index ),
                dir = ( index > Main._appIndex );
            
            $ov.animate({ opacity: 0, left: ( dir ? -200 : 200 )}, function() {
                $ov.css({ display: 'none' });
                $nv.css({ display: 'block', opacity: 0 , left: ( dir ? 200 : -200 )}).animate({ opacity: 1, left: 0 });
            });
            
            $oc.animate({ opacity: 0, left: ( dir ? 545 : 945 )}, function() {
                $oc.css({ display: 'none' });
                $nc.css({ display: 'block', opacity: 0, left: ( dir ? 945 : 545 )}).animate({ opacity: 1, left: 745 });
            });
            
            Main._$appMenu.removeClass( 'on' );
            Main._$appMenu.eq( index ).addClass( 'on' );
            Main._$appPrev[ index === 0 ? 'hide' : 'show' ]();
            Main._$appNext[ index === 2 ? 'hide' : 'show' ]();
            Main._appIndex = index;
        }
    },
    
    /**
     * 이전 앱 탭
     * 
     * @function
     */
    prevAppTab: function() {
        Main.changeAppTab( ( Main._appIndex || 0 ) - 1 );
    },
    
    /**
     * 다음 앱 탭
     * 
     * @function
     */
    nextAppTab: function() {
        Main.changeAppTab( ( Main._appIndex || 0 ) + 1 );
    },
    
    /**
     * <pre>
     * 전체 포인트 조회
     * </pre>
     * 
     * @function
     */
    ajaxWholePoint: function() {
        var $total = $( '#d_whole_total' );
        
        $.ajax({
            url: '/web/point/getWholePoint.do',
            type: 'POST',
            dataType: 'json',
            success: function( json ) {
                if ( json.result ) {
                    $total.text( number_format( json.data.wholePoint ) );
                } else {
                    alert( json.err_message );
                }
            }
        });
    },
    
    /**
     * <pre>
     * 나의 포인트 조회
     * </pre>
     * 
     * @function
     */
    ajaxMyPoint: function() {
        var $my = $( '#d_mypoint_my' ),
            $total = $( '#d_mypoint_total' ),
            $used = $( '#d_mypoint_used' ),
            $avail = $( '#d_mypoint_avail' ),
            maxPoint = 2500,
            maxGraph = 250;
        
        $.ajax({
            url: '/web/point/getMyPoint.do',
            type: 'POST',
            dataType: 'json',
            success: function( json ) {
                if ( json.result ) {
                    var t = json.data.totalPoint,
                        u = json.data.usedPoint,
                        a = json.data.availPoint,
                        f = function( n ) {
                            return Math.min( ( ( ( n ? Math.max( n, 200 ) : 0 ) / maxPoint ) * maxGraph ), maxGraph ) - maxGraph;
                        };
                    
                    $total.css( 'backgroundPosition', f( t ) + 'px 0' );
                    $used.css( 'backgroundPosition', f( u ) + 'px 0' );
                    $avail.css( 'backgroundPosition', f( a ) + 'px 0' );
                    
                    $my.text( number_format( t ) );
                    $total.children( 'em' ).text( number_format( t ) );
                    $used.children( 'em' ).text( number_format( u ) );
                    $avail.children( 'em' ).text( number_format( a ) );
                } else {
                    alert( json.err_message );
                }
            }
        });
    }
};


// ======================================== //
// 페이지 로드                               //
// ======================================== //
$(function() {
    var $window = $( window );
        
    // 윈도우 리사이즈
    (function() {
        var $sections = $( '.section1, .section2, .section3, .section4, .section5' );
        
        if ( $( 'body.mobile' )[ 0 ] ) {
            $window.on( 'resize.main', function() {
                Main.gnbTop = Main.gnbCon = [
                    $sections.eq( 1 ).offset().top,
                    $sections.eq( 2 ).offset().top,
                    $sections.eq( 3 ).offset().top,
                    $sections.eq( 4 ).offset().top
                ];
                
                $sections.not( '.section4' ).height( Math.min( $window.height(), 1200 ) );
            }).trigger( 'resize.main' );
        }
    })();
    
    // 윈도우 스크롤
    $window.on( 'scroll.main', function( event ) {
        Main.updateGnbUI();
    }).trigger( 'scroll.main' );
    
    // GNB 메뉴
    $( '#header .gnb li a, #d_banner1' ).click(function( event ) {
        event.preventDefault();
        Main.moveGnb( ( this.href.match( /#.+/ ) || [] )[ 0 ] );
    });
    
    // GNB 해쉬
    Main.moveGnb( location.hash );
    
    // 앱 탭
    $( '#d_app_tab a' ).click(function( event ) {
        event.preventDefault();
        Main.changeAppTab( $( this ).parent().index() );
    });
    
    // 앱 이전
    $( '#d_app_prev' ).click(function( event ) {
        event.preventDefault();
        Main.prevAppTab();
    });
    
    // 앱 다음
    $( '#d_app_next' ).click(function( event ) {
        event.preventDefault();
        Main.nextAppTab();
    });
    
    // 이벤트
    $( '.d_event' ).click(function( event ) {
        event.preventDefault();
        Common.showLayer( this.href );
    });
    
    // 데이터 전환
    $( '#d_data_change' ).not( '.anon' ).click(function() {
        Common.showLayer( '/web/exchange/exchangePoint.do' );
    });
    
    // 경품 응모
    $( '#d_data_givaway' ).not( '.anon' ).click(function() {
        Common.showLayer( '/web/event/getGiftList.do' );
    });
    
    // 100% 교환권
    $( '#d_data_coupon' ).not( '.anon' ).click(function() {
        Common.showLayer( '/web/event/dataCoupon.do' );
    });
    
    // 적립/사용 상세
    $( '#d_point_detail' ).click(function( event ) {
        event.preventDefault();
        Common.showLayer( this.href );
    });
    
    // 눝 포인트 사용 Tip
    $( '#d_tip_btn' ).click(function( event ) {
        event.preventDefault();
        $( '#d_tip_lay' ).toggle();
    });
});